package com.njtcedu.microblog.common.repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import org.springframework.stereotype.Repository;

import com.njtcedu.microblog.common.web.domain.PageModel;

@Repository
public class AbstractRepository {

	protected HibernateAccess hibernateAccess;

	protected JDBCAccess jdbcAccess;

	private final String SYS_DATE_SQL = "select now() as now from dual";

	protected Date getSysDate() {
		Date sysDate = jdbcAccess.findUniqueResult(SYS_DATE_SQL, new RowMapper<Date>() {
					@Override
					public Date mapRow(ResultSet resultSet, int rowNum) throws SQLException {
						return resultSet.getDate("now");
					}
				});
		return sysDate;
	}
	
	 public <T> PageModel<T> createPageModel(int pageNo, int pageSize, long totalRecords, List<T> records) {
	        PageModel<T> pageModel = new PageModel<T>();
	        pageModel.setPageNo(pageNo);
	        pageModel.setPageSize(pageSize);
	        pageModel.setRecords(records);
	        pageModel.setTotalRecords(totalRecords);
	        return pageModel;
	    }
	 
	@Inject
	public void setHibernateAccess(HibernateAccess hibernateAccess) {
		this.hibernateAccess = hibernateAccess;
	}

	@Inject
	public void setJdbcAccess(JDBCAccess jdbcAccess) {
		this.jdbcAccess = jdbcAccess;
	}

}
